from utils.Py_Mysql import Py_Mysql
from utils.Movie_requests import Movie_type,Movie_vod
from utils.get_ini import movieurl_select_sql,movievod_create_sql,movieurl_update_sql2
import configparser
from utils.log import logger
from concurrent.futures import ThreadPoolExecutor
config = configparser.RawConfigParser()
config.read('./config/config_sql.ini')
mysql = Py_Mysql()
header = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
}
url_data=mysql.Select_All(movieurl_select_sql)

# def getdata(url, url_name, movievod_table, mysql, header):
#     movievod_table = 'movievod_%s' % (i[7])
#     mysql.Create(movievod_create_sql % movievod_table, movievod_table)
#     for j in range(1, i[3]):
#         url = i[1].split('?')[0] + '?ac=videolist&pg=' + str(j)
#         Movie_vod(url, url_name, movievod_table, mysql, header)
#         logger.info(f'{url_name}:第{j}页电影数据爬取完成')
#     mysql.Update(movieurl_update_sql2 % (1, i))
#     logger.info(f'{url_name}:第一次电影数据爬取完成')
def test(i):
    print(i)

if __name__ == '__main__':
    # pool = ThreadPoolExecutor(max_workers=20)
    for i in url_data:
        if i[5]==0:
            #爬取全部
            movievod_table = 'movievod_%s' % (i[7])
            mysql.Create(movievod_create_sql % movievod_table, movievod_table)
            for j in range(1,i[3]):
                url=i[1].split('?')[0]+'?ac=videolist&pg='+str(j)
                args=[url, i[2], movievod_table, mysql, header]
                # pool.submit(Movie_vod,url, i[2], movievod_table, mysql, header)
                movie_vod = Movie_vod(args)
                logger.info(f'{i[2]}:第{j}页电影数据爬取完成')
            mysql.Update(movieurl_update_sql2 % (1, i[2]))
            logger.info(f'{i[2]}:第一次电影数据爬取完成')
        else:
            #爬取当天
            movievod_table='movievod_%s' % (i[7])
            mysql.Create(movievod_create_sql%movievod_table, movievod_table)
            url=i[1].split('?')[0]+'?ac=videolist&pg=&h=24&ids=&wd='
            Movie_vod(url, i[2], movievod_table, mysql, header)
            logger.info(f'{i[2]}:当天电影数据爬取完成')
    # pool.shutdown()